import { getUserInfo } from "../../utils/storage";
import { lockChannel, LockDayDto } from '../../api/personalChannel/index'
import { lockClue } from '../../api/newCarClue/index'
import { getLockService } from '../../api/lockService/index'
// import { lockPeer } from '../../api/peerCar/index'
import config from "../../config/index"
import { lockPeer } from "../../api/peerCar/index";

Component({
  properties: {
    lockShow: {
      type: Boolean,
      value: false
    },
    lockId: {
      type: String
    },
    key: {
      type: String,
      value: 'channel'
    }
  },
  data: {
    currentDay: {} as ListLockService,
    _index: 0,
    user: {} as WxUser,
    lockDay: [] as ListLockService[]
  },
  lifetimes: {
    attached() {
      this.initData()
    }
  },
  methods: {
    compare(lockA: ListLockService, lockB: ListLockService): number{
      return lockA.lockNum - lockB.lockNum
    },
    async initData() {
      const user = getUserInfo()
      const res = await getLockService()
      if(res.statusCode === config.SUCCESS_CODE) {
        let resList = res.data as ListLockService[]
        resList.sort(this.compare)
        this.setData({
          lockDay: resList,
          user,
          currentDay: resList[0] as ListLockService
        })
      }
      
    },
    chooseService(e: any) {
      this.setData({
        currentDay: e.currentTarget.dataset.item,
        _index: e.currentTarget.dataset.index
      })
    },
    onClose() {
      this.setData({
        lockShow: false
      })
      this.triggerEvent('show', this.properties.lockShow)
    },
    lock() {
      let data: LockDayDto = {
        id: this.properties.lockId,
        userId: this.data.user.id,
        lockDay: this.data.currentDay.lockNum,
        useCoin: this.data.currentDay.useCoin
      }
      if(data.useCoin > this.data.user.coin) {
        wx.showToast({title: '积分不足', icon: 'error'})
        this.onClose()
        this.triggerEvent('choose', true)
        return
      }
      switch(this.properties.key) {
        case 'channel':
          lockChannel(data).then((res) => {
            if(res.statusCode === config.CREATED_CODE) {
              wx.showToast({
                title: '锁定成功',
                icon: 'success'
              })
              this.onClose()
              setTimeout(() => {
                wx.reLaunch({
                  url: `/pages/personalChannelDetail/personalChannelDetail?id=${this.data.lockId}`
                })
              }, 1000)
            }
          })
          break
        case 'clue':
          lockClue(data).then((res: any) => {
            if(res.statusCode === config.CREATED_CODE) {
              wx.showToast({
                title: '锁定成功',
                icon: 'success'
              })
              this.onClose()
              setTimeout(() => {
                wx.reLaunch({
                  url: `/pages/newCarClueDetail/newCarClueDetail?id=${this.data.lockId}`
                })
               }, 1000)
            }
          })
          break
        case 'peer':
          lockPeer(data).then((res: any) => {
            if(res.statusCode === config.CREATED_CODE) {
              wx.showToast({
                title: '锁定成功',
                icon: 'success'
              })
              this.onClose()
              setTimeout(() => {
                wx.reLaunch({
                  url: `/pages/peerCarDetail/peerCarDetail?id=${this.data.lockId}`
                })
               }, 1000)
            }
          })
      }
    }
  }
})
